cssmatcher: Speed up common matching
authorBenjamin Otte <otte@redhat.com>
Thu, 28 May 2015 15:40:55 +0000 (17:40 +0200)
committerBenjamin Otte <otte@redhat.com>
Thu, 28 May 2015 15:41:47 +0000 (17:41 +0200)
first-child and last-child are the most common usages of the nth-child
machinery, so special-casing them makes sense.

gtk/gtkcssmatcher.c

index 14597ccf1b94b82bfb38461a84855243d0cd2b55..a15232764b8d61f62e00e9f300453b3e7ddb07dd 100644 (file)
@@ -358,6 +358,18 @@ gtk_css_matcher_node_nth_child (GtkCssNode *node,
 {
   int pos, x;
 
+  /* special-case the common "first-child" and "last-child" */
+  if (a == 0)
+    {
+      while (b > 0 && node != NULL)
+        {
+          b--;
+          node = prev_node_func (node);
+        }
+
+      return b == 0 && node == NULL;
+    }
+
   /* count nodes */
   for (pos = 0; node != NULL; pos++)
     node = prev_node_func (node);
@@ -366,9 +378,6 @@ gtk_css_matcher_node_nth_child (GtkCssNode *node,
    * and return TRUE if X is integer >= 0 */
   x = pos - b;
 
-  if (a == 0)
-    return x == 0;
-
   if (x % a)
     return FALSE;